package airAgent.group2;

import airAgent.group2.util.Util;

import ontology.ontology.AirAgentOntology;
import ontology.ontology.SubscribeTimer;
import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.ArrayList;

public class Timer extends Agent {
	private Codec codec = new SLCodec();
	private Ontology ontology = AirAgentOntology.getInstance();
	public static int day = 1;

	private static final long serialVersionUID = 1L;
	private ArrayList subscriberList = new ArrayList();

	protected void setup() {

		// Register of the codec and the ontology to be used in the
		// ContentManager
		getContentManager().registerLanguage(codec);
		getContentManager().registerOntology(ontology);

		Util.register(this.getName(), Util.TIMER, this);

		// GET SUBSCRIBE REQUESTS FROM AIRLINES
		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = 1L;

			public void action() {
				ACLMessage msg = receive(MessageTemplate.and(
						MessageTemplate.MatchLanguage(codec.getName()),
						MessageTemplate.MatchOntology(ontology.getName())));
				if (msg != null) {
					ContentElement ce = null;
					if (msg.getPerformative() == ACLMessage.SUBSCRIBE) {
						try {
							ce = getContentManager().extractContent(msg);
						} catch (UngroundedException e) {
							e.printStackTrace();
						} catch (CodecException e) {
							e.printStackTrace();
						} catch (OntologyException e) {
							e.printStackTrace();
						}
						if (ce instanceof Action) {
							Action agAction = (Action) ce;
							Concept con = agAction.getAction();

							ACLMessage reply = msg.createReply();
							reply.setLanguage(codec.getName());
							reply.setOntology(ontology.getName());
							String sender = msg.getSender().getLocalName();

							if (con instanceof SubscribeTimer) {
								reply.setProtocol(AirAgentOntology.SUBSCRIBETIMER);
								if (true) {
									//System.out.println("Subscribed agent: " + sender);
									reply.setPerformative(ACLMessage.AGREE);
									subscriberList.add(sender);
								} // else {
									// reply.setPerformative(ACLMessage.REFUSE);
									// }
							}
							myAgent.send(reply);
						}
					}
				}
			}
		});

		// START SIMULATION. SEND DATES
		addBehaviour(new TickerBehaviour(this, Util.TIME_SPEED_RATE) {
			private static final long serialVersionUID = 1L;
			//private boolean done;

			public void onTick() {
				int numSubs = 0;
				numSubs = subscriberList.size();

				if (numSubs > Util.NumAirlines) {
					for (int i = 0; i < numSubs; i++) {
						String currentSubscriber = (String) subscriberList
								.get(i);
						AID currentSubsAID = new AID(currentSubscriber,
								AID.ISLOCALNAME);
						if (day == 1)
							Util.StartSimulation(myAgent,
									currentSubsAID, codec, ontology);
						Util.InformDate(myAgent, currentSubsAID, codec,
								ontology);
						if (day == Util.SIMULATION_DAYS) {
							Util.EndSimulation(myAgent, currentSubsAID,
									codec, ontology);
							//done = true;
							stop();
						}
					}
					// done=true;
					System.out
							.println("-_-_-_-_-_-_-_-_TIMER changed date, new DAY: "
									+ day + "-_-_-_-_-_-_-_-_-_-_-_-_");
					day++;
				}
				/*new java.util.Timer().schedule(new java.util.TimerTask() {
					@Override
					public void run() {
						
					}
				}, (long) 10000);*/

			}

			/*@Override
			public boolean done() {
				return done;
			}*/

		});

	}
}
